package com.example.controller.admin;

import com.example.dto.CategoryPageQueryDTO;
import com.example.dto.InventoryPageQueryDTO;
import com.example.dto.InventoryPageQueryDTO2;
import com.example.entity.Inventory;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.InventoryService;
import com.example.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Inventory)表控制层
 *
 * @author makejava
 * @since 2024-03-21 20:13:56
 */
@RestController
@RequestMapping("/admin/inventory")
@Slf4j
@Api(tags = "库存相关接口")
public class InventoryController {
    /**
     * 服务对象
     */
    @Resource
    private InventoryService inventoryService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询库存")
    public Result<Inventory> queryById(@PathVariable("id") Long id) {
        return Result.success(this.inventoryService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param inventory 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("添加库存")
    public Result add(@RequestBody Inventory inventory) {
        log.info("添加库存，{}",inventory);
        Inventory i = inventoryService.insert(inventory);
        if(i==null){
            return Result.error("添加失败");
        }
        redisUtil.cleanRedis();
        return Result.success(i);
    }

    /**
     * 编辑数据
     *
     * @param inventory 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("修改库存")
    public Result update(Inventory inventory) {
        log.info("修改库存，{}",inventory);
        inventoryService.update(inventory);
        redisUtil.cleanRedis();
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("商品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("商品批量删除：{}",ids);
        inventoryService.delete(ids);
        redisUtil.cleanRedis();
        return Result.success();
    }

    /**
     * 库存分页查询
     * @param inventoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("库存分页查询,不能根据产品名字查询")
    public Result<PageResult> page(InventoryPageQueryDTO inventoryPageQueryDTO){
        log.info("分页查询：{}", inventoryPageQueryDTO);
        PageResult pageResult = inventoryService.pageQuery(inventoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 库存分页查询
     * @param inventoryPageQueryDTO
     * @return
     */
    @GetMapping("/page2")
    @ApiOperation("库存分页查询,可以根据产品名字查询")
    public Result<PageResult> page2(InventoryPageQueryDTO2 inventoryPageQueryDTO){
        log.info("分页查询：{}", inventoryPageQueryDTO);
        PageResult pageResult = inventoryService.pageQuery2(inventoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("库存状态修改")
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        inventoryService.startOrStop(status,id);
        redisUtil.cleanRedis();
//        cleanCache("product_*");
        return Result.success();
    }

}

